//
//  UIColorExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/2/20.
//

import UIKit

class UIColorExtension: NSObject {

}

extension UIColor {
    
    
    static func customRandomColor() -> (UIColor,UIColor) {
        
        let backgroundColors: [String] = ["#F5F6F8","#FFFBEF","#FFEDD6",
                                "#EEEDF9","#EEF3FC","#E9F6F0",
                                "#EAF4E1","#F5FFEF","#FFECF7",
                                "#E3F6FF","#DAEDEE","#FFEFC4",
                                "#F5F8F5","#FFCDCD","#FFDED9",
                                "#F8F1EC","#F2F5F5","#F6F0E9",
                                "#FFF7F7","#FCF5E1","#F1EFFF",
                                "#F4E3FC","#F0EEFC","#DEF6F5"]
        let textColors: [String] = ["#7585AD","#88661B","#997B4E",
                                     "#8781C5","#749BAE","#7D9472",
                                     "#3B6E23","#6A81AA","#9A5D72",
                                     "#749AD8","#5389A2","#BF7429",
                                     "#7A907A","#B4705E","#8B6758",
                                     "#CE6F13","#5D8192","#5B4A12",
                                     "#C06161","#BD953D","#6C6D80",
                                     "#BD55BF","#6E7296","#588B84"]
        let randomNumber = Int.random(in: 0..<backgroundColors.count)
        let bgColorHex = backgroundColors[randomNumber]
        let textColorHex = textColors[randomNumber]
        let bgColor = color(bgColorHex)
        let textColor = color(textColorHex)
        return (bgColor,textColor)
    }
    
    static func commonColor() -> UIColor {
        return color("#F5F6F8")
    }
    
    static func randomColor() -> UIColor {
        let red = CGFloat.random(in: 0...1)
        let green = CGFloat.random(in: 0...1)
        let blue = CGFloat.random(in: 0...1)
//        let alpha = CGFloat.random(in: 0...1)
        let alpha = CGFloat.random(in: 0.5...1)
        let randomColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
        return randomColor
    }
    
    
    convenience init (rColor: CGFloat, gColor: CGFloat, bColor: CGFloat, alpha: CGFloat) {
        self.init(red: CGFloat(rColor)/255.0, green: CGFloat(gColor)/255.0, blue: CGFloat(bColor)/255.0, alpha: 1.0)
    }
    
    convenience init(hexString: String, alpha: CGFloat = 1.0) {
        
        var cString = hexString.uppercased().trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        
        let length = (cString as NSString).length
        
        if length < 6 || length > 7 || (!cString.hasPrefix("#") && length == 7) {
            self.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
            return
        }
        
        if cString.hasPrefix("#") {
            cString = (cString as NSString).substring(from: 1)
        }
        
        var range = NSRange()
        range.location = 0
        range.length = 2
        
        let rString = (cString as NSString).substring(with: range)
        
        range.location = 2
        let gString = (cString as NSString).substring(with: range)
        
        range.location = 4
        let bString = (cString as NSString).substring(with: range)
        
        var red: UInt64 = 0, green: UInt64 = 0, blue: UInt64 = 0
        
        Scanner(string: rString).scanHexInt64(&red)
        Scanner(string: gString).scanHexInt64(&green)
        Scanner(string: bString).scanHexInt64(&blue)
        
        self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
    }
    
    /// 横向(左-->右)渐变色
    /// - Parameters:
    ///   - leftColor: 最左点颜色
    ///   - rightColor: 最右点颜色
    ///   - colorFrame: 尺寸
    /// - Returns: 渐变色
    class func horizontalGradientColor(leftColor: UIColor, rightColor: UIColor, colorFrame: CGRect) -> UIColor {
        
        let colorView = UIView()
        colorView.frame = colorFrame

        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [leftColor.cgColor, rightColor.cgColor]
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
        gradientLayer.frame = colorView.frame
        colorView.layer.insertSublayer(gradientLayer, at: 0)
        let image = colorView.convertToUIImage()
        return UIColor.init(patternImage: image)
    }
    
    /// 纵向(上-->下)渐变色
    /// - Parameters:
    ///   - leftColor: 最上面点颜色
    ///   - rightColor: 最下面点颜色
    ///   - colorFrame: 尺寸
    /// - Returns: 渐变色
    class func verticalGradientColor(topColor: UIColor, bottomColor: UIColor, colorFrame: CGRect) -> UIColor {
        
        let colorView = UIView()
        colorView.frame = colorFrame

        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 0, y: 1)
        gradientLayer.frame = colorView.frame
        colorView.layer.insertSublayer(gradientLayer, at: 0)
        let image = colorView.convertToUIImage()
        return UIColor.init(patternImage: image)
    }
    
    
}
